/*
 * MockInboundProcessor.java
 *
 * Created on May 7, 2009, 12:00 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.naga.bulksender.callback;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.naga.bulksender.data.SMSBulkMessage;
import org.smslib.InboundMessage;
import org.smslib.*;
import org.smslib.Service;
import org.apache.commons.lang.StringUtils;
import java.util.*;

/**
 * Mock Example of Custom Callback
 * This callback must to implement the InboundProcessor and define the logic to be executed once the message is recieved
 * @author Naga Services
 */
public class MockInboundProcessor implements InboundProcessor{
    private Log log = LogFactory.getLog(MockInboundProcessor.class);
    private Service srvSms = null;
    private  final Map<String,String> mockMap = new HashMap<String,String>();
    /** Creates a new instance of MockInboundProcessor */
    public MockInboundProcessor() {
        // We create a Dummy DataBase
        mockMap.put("10","Kiko Rasgado");
        mockMap.put("20","Maderito Guzman");
        mockMap.put("30","Patricio Perez");
    }
    
    public void processInboundMessage(String gateWay, Message.MessageTypes messageTypes, Message newMsg) {
        // We receive a Msg wi teh follow text    SERVICE_NAME ID_USER
        InboundMessage smsMsg = (InboundMessage)newMsg;
        
        log.info("Processing the Custom CallBack to be Integrated with other App");
        log.info("Text Received :> " + smsMsg.getText() );
        String textRec = smsMsg.getText() ;
        String[] token = StringUtils.split(textRec, " ");
        //We get the User Id and we search the information
        String answer = mockMap.get(token[1]);
        if ( answer != null){
            //If we found the answer, we will reply the msg to the user, with the information required
            String originator = smsMsg.getOriginator();
            OutboundMessage outMsg = new OutboundMessage(originator,answer);
            outMsg.setId(smsMsg.getId());
            srvSms.queueMessage(outMsg);
        }
    }
    
    public void setServiceProvider(Service svc){
        this.srvSms = svc;
    }
}
/**
 * You define in the Applciation Context the service name, 
 *
 *
  <bean id="inCallback" class="org.naga.bulksender.callback.InboundMessageCallback">
        <property name="messageDao" ref="messageDao"/>
        <property name="serviceProvider" ref="serviceProvider"/>
        <property name="mapProcessor">            
            <map>
                <entry key="DEMO">
                    <bean id="mockProcessor" class="org.naga.bulksender.callback.MockInboundProcessor">
                            <property name="serviceProvider" ref="serviceProvider"/>
                    </bean>
                </entry>
            </map>
            
        </property>
    </bean>
 *
 * In this case the message received will be 
 *      DEMO 10 or
 *      DEMO 20 or
 *      DEMO 30
 */